package liujia.bt.display.base
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import liujia.bt.display.BtDisplay;
	import liujia.bt.display.base.vo.ListenerVO;
	import liujia.bt.display.core.BtAnalyse;
	import liujia.bt.display.core.BtFrame;
	import liujia.bt.display.core.BtRun;
	import liujia.bt.display.interf.IBtAction;
	import liujia.bt.display.interf.IBtFrameRate;
	import liujia.bt.display.interf.IBtMouse;
	import liujia.bt.display.interf.IBtTransform;
	
	
	/**
	 * BtAnim
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtAnimMax.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtAnimMax extends BtAnim implements IBtTransform
	{
		
		protected var _mc:MovieClip = null;
		public function get mc():MovieClip {
			return _mc;
		}
		public function set mc(value:MovieClip):void {
			_mc = value;
		}
		
		protected var _useSourceTransform:Boolean = false;
		public function get useSourceTransform():Boolean {
			return _useSourceTransform;
		}
		public function set useSourceTransform(value:Boolean):void {
			_useSourceTransform = value;
		}
		
		protected var translatedFrameDic:Dictionary = new Dictionary();
		
		public function BtAnimMax(bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			super(bitmapData, pixelSnapping, smoothing);
		}
		
		override public function enterFrame():void {
			if (isDrag && parent) {
				if (isLockCenter) {
					var localPoint:Point = parent.globalToLocal(BtRun.mousePoint);
					set_x = Math.round(localPoint.x);
					set_y = Math.round(localPoint.y);
				} else {
					set_x += Math.round(BtRun.mouseMoveX);
					set_y += Math.round(BtRun.mouseMoveY);
				}
			}
			
			if (_isPlaying) {
				if (BtRun.nowTime - oldTime >= _ms) {
					oldTime = BtRun.nowTime;
				} else {
					return;
				}
				if (++_currentFrame > _totalFrames) _currentFrame = 1;
				
				//实验性功能
				if (_useSourceTransform /*&& (_rotation!=0 || _scaleX!=1 || _scaleY!=1)*/ && _currentFrame<=mc.totalFrames) {
					currentBtFrame = sourceTransform();
					render();
					updateXY();
				} else {
					tempBtFrame = _btFrameVec[_currentFrame-1];
					if (tempBtFrame != _currentBtFrame) {
						_currentBtFrame = tempBtFrame;
						render();
						updateXY();
					}
				}
				
				if (isPlayOnce && (currentFrame==totalFrames)) {
					stop();
				}
			}
			
		}
		
		protected var _isTranslated:Boolean = false;
		protected var _rotation:Number = 0;
		protected var _scaleX:Number = 1;
		protected var _scaleY:Number = 1;
		protected var _width:Number = 0;
		protected var _height:Number = 0;
		
		protected function sourceTransform():BtFrame {
			var btFrame:BtFrame = translatedFrameDic[_currentFrame];
			if (btFrame) {
				return btFrame;
			}
			mc.gotoAndStop(_currentFrame);
			mc.rotation = _rotation;
			mc.scaleX = _scaleX;
			mc.scaleY = _scaleY;
			btFrame = BtAnalyse.createFrame(mc);
			translatedFrameDic[_currentFrame] = btFrame;
			
			return btFrame;
		}
		
		protected function clearTranslatedFrameDic():void {
			for (var key:* in translatedFrameDic) {
				var btFrame:BtFrame = translatedFrameDic[key];
				if (btFrame) {
					btFrame.bitmapData.dispose();
					btFrame.bitmapData = null;
					btFrame.rect = null;
					translatedFrameDic[key] = null;
				}
			}
		}
		
		/**
		 *旋转角度
		 * @return 
		 * 
		 */
		override public function get rotation():Number {
			return _rotation;
		}
		/**
		 *旋转角度
		 * @return 
		 * 
		 */
		override public function set rotation(value:Number):void {
			_rotation = value;
			if (_useSourceTransform && _currentFrame<=mc.totalFrames) {
				currentBtFrame = sourceTransform();
				render();
				updateXY();
			} else {
				super.rotation = _rotation;
			}
			clearTranslatedFrameDic();
		}
		
		/**
		 *水平缩放 
		 * @return 
		 * 
		 */		
		override public function get scaleX():Number {
			return _scaleX;
		}
		/**
		 *水平缩放 
		 * @return 
		 * 
		 */
		override public function set scaleX(value:Number):void {
			_scaleX = value;
			if (_useSourceTransform && _currentFrame<=mc.totalFrames) {
				currentBtFrame = sourceTransform();
				render();
				updateXY();
			} else {
				super.scaleX = _scaleX;
			}
			clearTranslatedFrameDic();
		}
		
		/**
		 *垂直缩放 
		 * @return 
		 * 
		 */		
		override public function get scaleY():Number {
			return _scaleY;
		}
		/**
		 *垂直缩放 
		 * @return 
		 * 
		 */
		override public function set scaleY(value:Number):void {
			_scaleY = value;
			if (_useSourceTransform && _currentFrame<=mc.totalFrames) {
				currentBtFrame = sourceTransform();
				render();
				updateXY();
			} else {
				super.scaleY = _scaleY;
			}
			clearTranslatedFrameDic();
		}
		
		/**
		 *宽 
		 * @return 
		 * 
		 */		
		override public function get width():Number {
			return super.width;
		}
		
		/**
		 *宽 
		 * @return 
		 * 
		 */
		override public function set width(value:Number):void {
			_width = value;
			if (_useSourceTransform && _currentFrame<=mc.totalFrames) {
				currentBtFrame = sourceTransform();
				render();
				updateXY();
			} else {
				super.width = _width;
			}
			clearTranslatedFrameDic();
		}
		
		/**
		 *高
		 * @return 
		 * 
		 */
		override public function get height():Number {
			return super.height;
		}
		
		/**
		 *高
		 * @return 
		 * 
		 */
		override public function set height(value:Number):void {
			_height = value;
			if (_useSourceTransform && _currentFrame<=mc.totalFrames) {
				currentBtFrame = sourceTransform();
				render();
				updateXY();
			} else {
				super.height = _height;
			}
			clearTranslatedFrameDic();
		}
	}
}
